

# assemble_separators.pl
# This file is part of opengl-vrml-assembly.
# Copyright (C) Rafal Pielech, rafal.pielech@gmail.com

# opengl-vrml-assembly is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# opengl-vrml-assembly is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with opengl-vrml-assembly.  If not, see <http://www.gnu.org/licenses/>.


#
# Main
#

my $assembly_file_name = 'assembly.data';

# Delete the old gibot assembly file
unlink $assembly_file_name;

opendir(DIR, '.');
for my $vrml_separator_file (grep(/\.wrl$/,readdir(DIR)))
{  
  simplify_vrml_separator($vrml_separator_file);
}
closedir(DIR);


#
# Subroutines
#

sub simplify_vrml_separator
{
  my $vrml_file_name = shift;
  my $vrml_file_content;
  my $vrml_file_simplified = '';
  
  open IN, "<$vrml_file_name" or die "$!";
  $vrml_file_content = join('', <IN>);
  close IN;
  
  # Get points, indices
  if(!($vrml_file_content =~
       /Separator\s*{\s*Material\s*{.*?}\s*Coordinate3\s*{\s*point\s*\[(.*?)\]\s*}\s*IndexedFaceSet\s*{\s*coordIndex\s*\[(.*?)\]\s*}\s*}/s))
  {
    print "Error: $vrml_file_name does not have VRML Separator format.\n";
    return;
  }
  
  my $points  = $1;
  my $indices = $2;
  
  # POINTS
    $points =~ s/^\s*|,|\s*$//g;
    $points =~ s/\s+/ /g;
    my $points_counter = 0;
    while($points =~ /\S+/g)
    {
      $points_counter++;
    }
    # Each point has three coordinates
    $points_counter /= 3;
    $vrml_file_simplified .= "$points_counter $points ";
  
  # FACES
    $indices =~ s/^\s*|,|\s*$//g;
    $indices =~ s/\s+/ /g;
    $indices =~ s/-1/EOF/g;
    
    for (my $i=0; $i<2; $i++)
    {
      my $faces = '';
      my $faces_counter = 0;
      
      my $face_regexp;
      if($i == 0) # Triangles
      {
        $face_regexp = '([0-9]+ [0-9]+ [0-9]+) EOF';
      }
      else
      {
        if($i == 1) # Quads
        {
          $face_regexp = '([0-9]+ [0-9]+ [0-9]+ [0-9]+) EOF';
        }
      }
      
      while($indices =~ /$face_regexp/g)
      {
        $faces .= "$1 ";
        $faces_counter++;
      }
      if($faces_counter > 0)
      {
        $faces =~ s/\s*$//;
        $vrml_file_simplified .= "$faces_counter $faces ";
      }
      else
      {
        # Output information that there are no faces
        $vrml_file_simplified .= "$faces_counter ";
      }
    }
  
  print "$vrml_file_name has just been simplified.\n";
  $vrml_file_simplified =~ s/\s*$//;
  open OUT, '>>', $assembly_file_name or die $!;
  $vrml_file_name =~ /(^.*?)\./;
  $vrml_file_simplified = join('', '{', '(', $1, ')', $vrml_file_simplified, '}');
  print OUT length($vrml_file_simplified), $vrml_file_simplified;
  close OUT; 
}
